<?php

class Product_model extends MY_Model {

    function __construct() {
        parent::__construct();
        $this->table = 'product';
    }
    function getSearch($params, $sort = 'id', $order = 'asc') {
    	$this->db->select('product.*');
    	if ( isset($params['product_name']) && $params['product_name'] != '') {
    		$this->db->like('product.product_name', trim($params['product_name']), 'both');
    	}
    	if ($params['category_id'] != '') {
    		$this->db->where('product.category_id', $params['category_id']);
    	}
    	if ($params['provider_id'] != '') {
    		$this->db->where('pp.provider_id', $params['provider_id']);
    	}
    	$this->db->join("product_provider pp","pp.product_id = product.id");
    	$this->db->order_by('product.' . $sort, $order);
    	$this->db->order_by('product.update_date', 'desc');
    	$query = $this->db->get($this->table);
    	return $query->result();
    }
    function getWithProvider( $sort = 'id', $order = 'asc') {
    	$this->db->select('product.*, pp.provider_id');
    	$this->db->join("product_provider pp","pp.product_id = product.id");
    	$this->db->order_by('product.' . $sort, $order);
    	$this->db->order_by('product.update_date', 'desc');
    	$query = $this->db->get($this->table);
    	return $query->result();
    }
}

?>